package org.exhibition.controller;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.common.util.JwtUtils;
import org.common.util.Result;
import org.common.util.ThreadlocalUtils;
import org.exhibition.domain.pojo.exhibition.ExhibitionOrder;
import org.exhibition.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/exhibition")
@CrossOrigin
public class exhibitionController {
    @Autowired
    private ExhibitionService exhibitionService;

    //获取展览的list数据
    @GetMapping
    public Result getAllExhibition(){
        //System.out.println("111");
        return Result.success(exhibitionService.getAllExhibition());
    }

    @GetMapping("/{id}")
    //根据id获取展览
    public Result getExhibitionById(@PathVariable("id")Integer id){
        return Result.success(exhibitionService.getExhibitionById(id));
    }

    @GetMapping("/getExhibitionByDate")
    //根据日期获取展览
    public Result getExhibitionByDate(@RequestParam("startDate") LocalDate startDate){
        return Result.success(exhibitionService.getExhibitionByDate(startDate));
    }

    @GetMapping("/getExhibitionByName/{name}")
    //根据名称获取展览
    public Result getExhibitionByName(@PathVariable("name") String name,@RequestParam("startDate") LocalDate startDate){
        Result result=Result.success();
        System.out.println(name+startDate);
        result.put("data",exhibitionService.getExhibitionByName(name,startDate));
        result.put("detail",exhibitionService.getExhibitionDetailByName(name,startDate));
        return result;
    }

    @PostMapping("/submitExhibitionReservation")
    //提交展览预约
    public Result submitExhibitionReservation(@RequestBody ExhibitionOrder exhibitionOrder) throws JsonProcessingException {
        System.out.println(exhibitionOrder);
        List<String> list=new ArrayList<>();
        for(int i=0;i<exhibitionOrder.getUserOrder().size();i++){
            exhibitionOrder.getUserOrder().get(i).setCreateTime(LocalDateTime.now().withNano(0));
            System.out.println(exhibitionOrder.getUserOrder().get(i));
            String res=exhibitionService.submitExhibitionReservation(exhibitionOrder.getUserOrder().get(i));
            if(!res.equals("预购成功")){
                list.add(exhibitionOrder.getUserOrder().get(i).getName()+res);
            }
        }
        if(list.isEmpty()){
            ThreadlocalUtils.remove();
            return Result.success("预交成功");
        }
        return Result.error("预购失败",list);
    }

    //进行展览的点赞
    @PostMapping("/likeExhibition/{exhibitionId}")
    public Result likeExhibition(@PathVariable("exhibitionId") Integer exhibitionId){
        System.out.println(exhibitionId);
        boolean res=exhibitionService.likeExhibition(exhibitionId);
        if(!res){
            return Result.error("点赞失败");
        }
        return Result.success("点赞成功");
    }
    //取消点赞
    @PostMapping("/cancelLikeExhibition/{exhibitionId}")
    public Result cancelLikeExhibition(@PathVariable("exhibitionId") Integer exhibitionId){
        System.out.println(exhibitionId);
        boolean res=exhibitionService.cancelLikeExhibition(exhibitionId);
        if(!res){
            return Result.error("取消点赞失败");
        }
        return Result.success("取消点赞成功");
    }

    //查询用户是否点赞
    @GetMapping("/isLikeExhibition/{exhibitionId}")
    public Result isLikeExhibition(@PathVariable("exhibitionId") Integer exhibitionId){
        String token=ThreadlocalUtils.get();
        //System.out.println(token.isEmpty());
        if(token.isEmpty()){
            return Result.success("没有点赞过");
        }
        String phone= JwtUtils.getJwtUsername(token);
        boolean res=exhibitionService.isLikeExhibition(phone,exhibitionId);
        if(res){
            return Result.error("已经点赞过了");
        }
        return Result.success("没有点赞过");
    }

    //根据展览名称获取id
    @GetMapping("/getExhibitionIdByName/{name}")
    public Result getExhibitionIdByName(@PathVariable("name") String name){
        System.out.println( name);
        return Result.success("查询成功",exhibitionService.getExhibitionIdByName(name));
    }

    //查询用户的展览预约订单信息
    @GetMapping("/getMyExhibitionReservation")
    public Result getMyExhibitionReservation(){
        return Result.success(exhibitionService.getAllExhibitionReservation());
    }


}
